package com.manager.commons.tag.statistics;



import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.manager.BaseService;
import com.manager.commons.StringUtil;




public class IndividualCurrencyTag extends TagSupport {
	private String sql;
	private String listName;//列表名 
	private String tableName;//表名
	private String condition;//条件
	private String conditionText;//用于显示查询条件
	private String type;//类型
	private String userName;
	private String orderState;
	private String orgName;
	private String orderClickUrl;
	private String orderColumn;
	private List args;//参数
	BaseService baseService;
	public int doStartTag() throws JspException {
		JspWriter out = pageContext.getOut();
		HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
		ServletContext servletContext = request.getSession().getServletContext();
		ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);
		baseService = (BaseService) ctx.getBean("baseService");
		try {
			if(type!=null&&type.equals("1")){
				out.print(this.getTable(baseService));
			}
			if(type!=null&&type.equals("2")){
				out.print(this.getTableType2(baseService));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return SKIP_BODY;
	} 
	private StringBuffer getTable(BaseService baseService){
		String[] arrListName = this.listName.split(",");
		StringBuffer tableTemp  = new StringBuffer();
		List listSqlTemp = new ArrayList();
		String startSql = this.sql;
		
		
		if(this.condition!=null&&!this.condition.equals("")){
			startSql = startSql + " "+ condition;
		}
		orgName=orgName==null?"":orgName;
		if(this.orderColumn!=null&&!this.orderColumn.equals("")){
			startSql = startSql + " "+orderColumn;
		}
		
		if(this.orderState!=null&&!this.orderState.equals("")){
			
			startSql+=" "+orderState;
		}
		
		if(null != args && args.size()>0){
			listSqlTemp = baseService.getListForPage(startSql,args);
		}else{
			listSqlTemp = baseService.getListForPage(startSql);
		}
		try {
			//转换编码 
			condition=java.net.URLEncoder.encode(condition,"UTF-8"); 
			conditionText=java.net.URLEncoder.encode(conditionText,"UTF-8"); 
			
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		int countWidth=0;
		for(int i=0;i<arrListName.length;i++){
			String[] arrTemp = arrListName[i].split("!");
				countWidth+=Integer.parseInt(arrTemp[2]);
			}
		Date date=new Date();
		String dateStr="";
		try {
			 dateStr=StringUtil.dateToString(date);
		} catch (Exception e) {
			// TODO 自动生成 catch 块 e.printStackTrace();
		}
		
		//设置标题
		//String titleName="金钼股份"+orgName+java.util.Calendar.getInstance().get(java.util.Calendar.YEAR)+tableName;
		
		String titleName=tableName;
		
		tableTemp.append("<table width="+countWidth+" cellspacing=\"1\" cellpadding=\"0\"  align=\"center\"  border=\"0\" style=\"BORDER-RIGHT: medium none; BORDER-TOP: medium none; BORDER-LEFT: medium none; BORDER-BOTTOM: medium none; BORDER-COLLAPSE: collapse; mso-table-layout-alt: fixed; mso-border-alt: solid black .5pt; mso-padding-alt: 0cm 5.4pt 0cm 5.4pt; mso-border-insideh: .5pt solid black; mso-border-insidev: .5pt solid black; mso-yfti-tbllook: 480\"  ><tr><td   style=\"font-size: 24px;font-weight: bold;align=center;\"  colspan="+arrListName.length+" align=\"center\" >"+titleName+"</td></tr></table>");
		tableTemp.append("<table width="+countWidth+" cellspacing=\"1\" cellpadding=\"0\"  align=\"center\"  border=\"0\"   ><tr><td width="+countWidth/2+"  style=\"font-size: 12px;align=center;\"  align=\"left\" >导出人:"+this.userName+"</td><td width="+countWidth/2+"  style=\"font-size: 12px;align=center;\"   align=\"right\" >导出时间:"+dateStr+"</td></tr></table>");
		
		tableTemp.append("<table width="+countWidth+" cellspacing=\"1\" cellpadding=\"0\"  align=\"center\"  border=\"0\" style=\"BORDER-RIGHT: medium none; BORDER-TOP: medium none; BORDER-LEFT: medium none; BORDER-BOTTOM: medium none; BORDER-COLLAPSE: collapse; mso-table-layout-alt: fixed; mso-border-alt: solid black .5pt; mso-padding-alt: 0cm 5.4pt 0cm 5.4pt; mso-border-insideh: .5pt solid black; mso-border-insidev: .5pt solid black; mso-yfti-tbllook: 480\"  >");
		
	
		tableTemp.append("<tr style=\"HEIGHT: 30.9pt; mso-yfti-irow: 0; mso-yfti-firstrow: yes\" align=\"center\">");
		String[] arrTempHj = new String[arrListName.length];
		for(int i=0;i<arrListName.length;i++){
			String[] arrTemp = arrListName[i].split("!");
			if(i==0){
				tableTemp.append("<td align=\"center\"   style=\"BORDER-RIGHT: black 1pt solid; WIDTH:"+arrTemp[2]+"pt; PADDING-RIGHT: 5.4pt; BORDER-TOP: black 1pt solid; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: black 1pt solid;  PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid; HEIGHT: 30.9pt; BACKGROUND-COLOR: transparent; mso-border-alt: solid black .5pt\">"+arrTemp[0]+"</td>");
			}else{
				
				tableTemp.append("<td align=\"center\"   style=\"BORDER-RIGHT: black 1pt solid; WIDTH:"+arrTemp[2]+"pt; PADDING-RIGHT: 5.4pt; BORDER-TOP: black 1pt solid; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: black 1pt solid;  PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid; HEIGHT: 30.9pt; BACKGROUND-COLOR: transparent; mso-border-alt: solid black .5pt\">" +
						"<a style=\"cursor:hand;\" onclick=\"locationhref('"+arrTemp[1]+"');\">"+arrTemp[0]+"</a></td>");
			}
			if(arrTemp.length>2){
				arrTempHj[i]="0";
			}
			else{
				arrTempHj[i]="&nbsp;";
			}
		}
		tableTemp.append("</tr>");
		int tempTotal = 0;
		for(int i=0;i<listSqlTemp.size();i++){
			tableTemp.append("<tr  style=\"HEIGHT: 30.9pt; mso-yfti-irow: 0; mso-yfti-firstrow: yes\">");
			for(int j=0;j<arrListName.length;j++){
				String[] arrTemp = arrListName[j].split("!");
				Map map=(Map)listSqlTemp.get(i); 
			    
				if(map.get(arrTemp[1])!=null && !map.get(arrTemp[1]).equals("")){
					
					String arrValue="";
					if (map.get(arrTemp[1]) instanceof BigDecimal) {
						DecimalFormat  df=new DecimalFormat("#########.######");
						arrValue=df.format(map.get(arrTemp[1]));
					}else{
						arrValue=map.get(arrTemp[1]).toString();
					}
					
					
					
					if(j==0){
						tableTemp.append("<td style=\"BORDER-RIGHT: black 1pt solid;WIDTH:"+arrTemp[2]+"pt; PADDING-RIGHT: 5.4pt; BORDER-TOP: black 1pt solid; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: black 1pt solid;  PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid; HEIGHT: 30.9pt; BACKGROUND-COLOR: transparent; mso-border-alt: solid black .5pt\">"+(i+1)+"</td>");
					
					}else{
						tableTemp.append("<td  style=\"BORDER-RIGHT: black 1pt solid;WIDTH:"+arrTemp[2]+"pt; PADDING-RIGHT: 5.4pt; BORDER-TOP: black 1pt solid; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: black 1pt solid;  PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid; HEIGHT: 30.9pt; BACKGROUND-COLOR: transparent; mso-border-alt: solid black .5pt\">"+arrValue+"</td>");
					}
					if(arrTemp.length>3&&arrTemp[3].equals("LH^D")){
						arrTempHj[j] = Double.parseDouble(arrTempHj[j].toString())+Double.parseDouble(map.get(arrTemp[1]).toString())+"";
						tempTotal = j;
					}
					if(arrTemp.length>3&&arrTemp[3].equals("LH^I")){
						arrTempHj[j] = Integer.parseInt(arrTempHj[j].toString())+ Integer.parseInt(map.get(arrTemp[1]).toString())+"";
						tempTotal = j;
					}
				}
				else{
					tableTemp.append("<td  style=\"BORDER-RIGHT: black 1pt solid;WIDTH:"+arrTemp[2]+"pt; PADDING-RIGHT: 5.4pt; BORDER-TOP: black 1pt solid; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: black 1pt solid;  PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid; HEIGHT: 30.9pt; BACKGROUND-COLOR: transparent; mso-border-alt: solid black .5pt\">&nbsp;</td>");
				}
			}
			tableTemp.append("</tr>");
		}
		
		
		tableTemp.append("<script language='javascript'>function locationhref(varc){" +
				
				"location.href('"+orderClickUrl+"&columnName='+varc+'&checkType=2&orderState="+this.orderState+"&condition="+condition+"&conditionText="+conditionText+"');" +
				"}</script>");
		
		//暂时处理只有一列合计的
		if(tempTotal!=0){
			tableTemp.append("<tr style=\"HEIGHT: 30.9pt; mso-yfti-irow: 0; mso-yfti-firstrow: yes\" >");
			tableTemp.append("<td align=\"right\"   style=\"BORDER-RIGHT: black 1pt solid;PADDING-RIGHT: 5.4pt; BORDER-TOP: black 1pt solid; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: black 1pt solid;  PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid; HEIGHT: 30.9pt; BACKGROUND-COLOR: transparent; mso-border-alt: solid black .5pt\" colspan=\""+tempTotal+"\"><strong><font color=\"#013794\">合计</font></strong>");
			tableTemp.append("<td align=\"left\"  style=\"BORDER-RIGHT: black 1pt solid;PADDING-RIGHT: 5.4pt; BORDER-TOP: black 1pt solid; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: black 1pt solid;  PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid; HEIGHT: 30.9pt; BACKGROUND-COLOR: transparent; mso-border-alt: solid black .5pt\"  colspan=\""+(arrTempHj.length - tempTotal)+"\"><strong><font color=\"#013794\">"+arrTempHj[tempTotal]+"</font></strong></tr>");
			
		}
//		tableTemp.append("</tr>");
		tableTemp.append("");
		tableTemp.append("</table>");
		return tableTemp;
	}
	
	private StringBuffer getTableType2(BaseService baseService){
		String[] arrListName = this.listName.split(",");
		StringBuffer tableTemp  = new StringBuffer();
		List listSqlTemp = new ArrayList();
		String startSql = this.sql;
		if(this.condition!=null&&!this.condition.equals("")){
			startSql = startSql + " where 1=1 and " + condition;
		}
		if(null != args && args.size()>0){
			listSqlTemp = baseService.getListForPage(startSql,args);
		}else{
			listSqlTemp = baseService.getListForPage(startSql);
		}
		tableTemp.append("<table cellspacing=\"1\" cellpadding=\"0\" width=\"100%\" align=\"center\"  border=\"0\" class=\"bgcolor1 margb5\" ><tr height=30><td align=\"center\" class=\"zi14\" colspan="+arrListName.length+" align=\"center\"><strong><font size=\"4\" color=\"#013794\">"+this.tableName+"</font></strong></tr></td>");
		tableTemp.append("<tr height=28>");
		String[] arrTempHj = new String[arrListName.length];
		for(int i=0;i<arrListName.length;i++){
			String[] arrTemp = arrListName[i].split("!");
			tableTemp.append("<td>&nbsp;&nbsp;<strong>"+arrTemp[0]+"</strong>&nbsp;&nbsp;</tr>");
			if(arrTemp.length>2){
				arrTempHj[i]="0";
			}
			else{
				arrTempHj[i]="&nbsp;";
			}
		}
		tableTemp.append("</tr>");
		for(int i=0;i<listSqlTemp.size();i++){
			tableTemp.append("<tr height=28>");
			for(int j=0;j<arrListName.length;j++){
				String[] arrTemp = arrListName[j].split("!");
				Map   map=(Map)listSqlTemp.get(i); 
				if(map.get(arrTemp[1])!=null){
					tableTemp.append("<td>"+map.get(arrTemp[1]).toString()+"</td>");
					if(arrTemp.length>2&&arrTemp[2].equals("LH^D")){
						arrTempHj[j] = Double.parseDouble(arrTempHj[j].toString())+Double.parseDouble(map.get(arrTemp[1]).toString())+"";
					}
					if(arrTemp.length>2&&arrTemp[2].equals("LH^I")){
						arrTempHj[j] = Integer.parseInt(arrTempHj[j].toString())+ Integer.parseInt(map.get(arrTemp[1]).toString())+"";
					}
				}
				else{
					tableTemp.append("<td>&nbsp;</td>");
				}
			}
			tableTemp.append("</tr>");
		}
		tableTemp.append("<tr height=28>");
		for(int i=0;i<arrListName.length;i++){
			tableTemp.append("<td align=\"right\"><strong><font color=\"#013794\">"+arrTempHj[i]+"</font></strong></tr>");
		}
		tableTemp.append("</tr>");
		tableTemp.append("");
		tableTemp.append("</table>");
		return tableTemp;
	}
	
	public String getCondition() {
		return condition;
	}
	public void setCondition(String condition) {
		this.condition = condition;
	}
	public String getListName() {
		return listName;
	}
	public void setListName(String listName) {
		this.listName = listName;
	}
	public String getSql() {
		return sql;
	}
	public void setSql(String sql) {
		this.sql = sql;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public String getTableName() {
		return tableName;
	}
	public void setTableName(String tableName) {
		this.tableName = tableName;
	}
	public List getArgs() {
		return args;
	}
	public void setArgs(List args) {
		this.args = args;
	}
	public String getOrderState() {
		return orderState;
	}
	public void setOrderState(String orderState) {
		this.orderState = orderState;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getOrderColumn() {
		return orderColumn;
	}
	public void setOrderColumn(String orderColumn) {
		this.orderColumn = orderColumn;
	}
	
	public String getOrderClickUrl() {
		return orderClickUrl;
	}
	public void setOrderClickUrl(String orderClickUrl) {
		this.orderClickUrl = orderClickUrl;
	}
	public String getOrgName() {
		return orgName;
	}
	public void setOrgName(String orgName) {
		this.orgName = orgName;
	}
	public String getConditionText() {
		return conditionText;
	}
	public void setConditionText(String conditionText) {
		this.conditionText = conditionText;
	}


}